﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{

    public static class HangHoa
    {

        public static Entities.HangHoa[] Select()
        {
            Entities.HangHoa[] arrC = null;
            try
            {
                string sql = "Select * from HangHoa where Deleted = 0";
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HangHoa row = new Entities.HangHoa();
                    row.HangHoaID = int.Parse(dr[Common.Constants.HangHoa.HangHoaID].ToString());
                    row.MaHangHoa = dr[Common.Constants.HangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.HangHoa.TenHangHoa].ToString();
                    row.MaNhomHang = dr[Common.Constants.HangHoa.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.HangHoa.TenNhomHang].ToString();
                    row.MaDonViTinh = dr[Common.Constants.HangHoa.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.HangHoa.TenDonViTinh].ToString();
                    row.GiaNhap = dr[Common.Constants.HangHoa.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.HangHoa.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.HangHoa.GiaBanLe].ToString();
                    row.MaThue = dr[Common.Constants.HangHoa.MaThue].ToString();
                    row.GiaTriThue = dr[Common.Constants.HangHoa.GiaTriThue].ToString();
                    row.MucDatHang = dr[Common.Constants.HangHoa.MucDatHang].ToString();
                    row.MucTonToiThieu = dr[Common.Constants.HangHoa.MucTonToiThieu].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage\" + dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Images = null;
                    row.GhiChu = dr[Common.Constants.HangHoa.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HangHoa.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { return null; }
                arrC = new Entities.HangHoa[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.HangHoa)arr[i];
                }

                //for (int i = 0; i < n; i++)
                //{
                //    arrC[i].TenNhomHangHoa = LayNhomHangHoa(arrC[i].MaNhomHangHoa);
                //    arrC[i].TenDonViTinh = LayDVT(arrC[i].MaDonViTinh);
                //    arrC[i].TenThueGiaTriGiaTang = LayThue(arrC[i].MaThueGiaTriGiaTang);

                //}
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }

            catch (Exception ex)
            { string s = ex.Message.ToString(); }
            return arrC;
        }

        public static Entities.HangHoa[] SelectHangHoa()
        {
            Entities.HangHoa[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.selectTatCaHangHoa, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.HangHoa row = new Entities.HangHoa();
                    row.HangHoaID = int.Parse(dr[Common.Constants.HangHoa.HangHoaID].ToString());
                    row.MaHangHoa = dr[Common.Constants.HangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.HangHoa.TenHangHoa].ToString();
                    row.MaNhomHang = dr[Common.Constants.HangHoa.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.HangHoa.TenNhomHang].ToString();
                    row.MaDonViTinh = dr[Common.Constants.HangHoa.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.HangHoa.TenDonViTinh].ToString();
                    row.GiaNhap = dr[Common.Constants.HangHoa.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.HangHoa.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.HangHoa.GiaBanLe].ToString();
                    row.MaThue = dr[Common.Constants.HangHoa.MaThue].ToString();
                    row.GiaTriThue = dr[Common.Constants.HangHoa.GiaTriThue].ToString();
                    row.MucDatHang = dr[Common.Constants.HangHoa.MucDatHang].ToString();
                    row.MucTonToiThieu = dr[Common.Constants.HangHoa.MucTonToiThieu].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage\" + dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Images = null;
                    row.GhiChu = dr[Common.Constants.HangHoa.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HangHoa.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.HangHoa[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.HangHoa)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        public static Entities.HangHoa[] SelectHangHoa(Entities.HangHoa nv)
        {
            Entities.HangHoa[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.selectTatHangHoa, cn);
                cmd.Parameters.Add(Common.Constants.HangHoa.HanhDong, SqlDbType.VarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.HangHoa row = new Entities.HangHoa();
                    row.HangHoaID = int.Parse(dr[Common.Constants.HangHoa.HangHoaID].ToString());
                    row.MaHangHoa = dr[Common.Constants.HangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.HangHoa.TenHangHoa].ToString();
                    row.MaNhomHang = dr[Common.Constants.HangHoa.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.HangHoa.TenNhomHang].ToString();
                    row.MaDonViTinh = dr[Common.Constants.HangHoa.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.HangHoa.TenDonViTinh].ToString();
                    row.GiaNhap = dr[Common.Constants.HangHoa.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.HangHoa.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.HangHoa.GiaBanLe].ToString();
                    row.MaThue = dr[Common.Constants.HangHoa.MaThue].ToString();
                    row.GiaTriThue = dr[Common.Constants.HangHoa.GiaTriThue].ToString();
                    row.MucDatHang = dr[Common.Constants.HangHoa.MucDatHang].ToString();
                    row.MucTonToiThieu = dr[Common.Constants.HangHoa.MucTonToiThieu].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage\" + dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Images = null;
                    row.GhiChu = dr[Common.Constants.HangHoa.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HangHoa.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.HangHoa[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.HangHoa)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        public static Entities.HangHoa[] SelectHangHoa1(Entities.HangHoa hanghoa)
        {
            Entities.HangHoa[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.sp_SelectHangHoa, cn);
                cmd.Parameters.Add(Common.Constants.HangHoa.HanhDong, SqlDbType.NVarChar, 50).Value = hanghoa.HanhDong;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaHangHoa, SqlDbType.VarChar, 50).Value = hanghoa.MaHangHoa;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.HangHoa row = new Entities.HangHoa();
                    row.HangHoaID = int.Parse(dr[Common.Constants.HangHoa.HangHoaID].ToString());
                    row.MaHangHoa = dr[Common.Constants.HangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.HangHoa.TenHangHoa].ToString();
                    row.MaNhomHang = dr[Common.Constants.HangHoa.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.HangHoa.TenNhomHang].ToString();
                    row.MaDonViTinh = dr[Common.Constants.HangHoa.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.HangHoa.TenDonViTinh].ToString();
                    row.GiaNhap = dr[Common.Constants.HangHoa.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.HangHoa.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.HangHoa.GiaBanLe].ToString();
                    row.MaThue = dr[Common.Constants.HangHoa.MaThue].ToString();
                    row.GiaTriThue = dr[Common.Constants.HangHoa.GiaTriThue].ToString();
                    row.MucDatHang = dr[Common.Constants.HangHoa.MucDatHang].ToString();
                    row.MucTonToiThieu = dr[Common.Constants.HangHoa.MucTonToiThieu].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\HangHoaImage\" + dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Images = null;
                    row.GhiChu = dr[Common.Constants.HangHoa.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HangHoa.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.HangHoa[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.HangHoa)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }


        public static int Insert(Entities.HangHoa hh)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.insertHangHoa, cn);
                cmd.Parameters.Add(Common.Constants.HangHoa.HanhDong, SqlDbType.NVarChar, 50).Value = hh.HanhDong;
                cmd.Parameters.Add(Common.Constants.HangHoa.HangHoaID, SqlDbType.Int).Value = hh.HangHoaID;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaHangHoa, SqlDbType.VarChar, 50).Value = hh.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaNhomHang, SqlDbType.VarChar, 50).Value = hh.MaNhomHang;
                cmd.Parameters.Add(Common.Constants.HangHoa.TenNhomHang, SqlDbType.NVarChar, 200).Value = hh.TenNhomHang;
                cmd.Parameters.Add(Common.Constants.HangHoa.TenDonViTinh, SqlDbType.NVarChar, 200).Value = hh.TenDonViTinh;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaDonViTinh, SqlDbType.VarChar, 50).Value = hh.MaDonViTinh;
                cmd.Parameters.Add(Common.Constants.HangHoa.TenHangHoa, SqlDbType.NVarChar, 200).Value = hh.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaNhap, SqlDbType.Float).Value = hh.GiaNhap;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaBanBuon, SqlDbType.Float).Value = hh.GiaBanBuon;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaBanLe, SqlDbType.Float).Value = hh.GiaBanLe;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaThue, SqlDbType.VarChar, 50).Value = hh.MaThue;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaTriThue, SqlDbType.Float).Value = hh.GiaTriThue;
                cmd.Parameters.Add(Common.Constants.HangHoa.MucDatHang, SqlDbType.Float).Value = hh.MucDatHang;
                cmd.Parameters.Add(Common.Constants.HangHoa.MucTonToiThieu, SqlDbType.Float).Value = hh.MucTonToiThieu;
                string linked = string.Empty;
                if (hh.LinkImage != string.Empty)
                {
                    linked = hh.MaHangHoa + ".jpeg";
                }
                else
                {
                    linked = string.Empty;
                }
                cmd.Parameters.Add(Common.Constants.HangHoa.LinkImage, SqlDbType.NVarChar).Value = linked;
                cmd.Parameters.Add(Common.Constants.HangHoa.GhiChu, SqlDbType.NVarChar, 200).Value = hh.GhiChu;
                cmd.Parameters.Add(Common.Constants.HangHoa.Deleted, SqlDbType.Bit).Value = hh.Deleted;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    if (linked != string.Empty)
                    {
                        string links = @"\HangHoaImage\" + linked;
                        string linksRoot = System.Windows.Forms.Application.StartupPath.ToString() + links;
                        try { Common.Utilities.DeleteFile(links); }
                        catch { }
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(hh.Images);
                        Bitmap bmp = new Bitmap(ms);
                        try
                        {
                            bmp.Save(linksRoot, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        catch { }
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
        public static int Update(Entities.HangHoa hh)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.updateHangHoa, cn);
                cmd.Parameters.Add(Common.Constants.HangHoa.HanhDong, SqlDbType.NVarChar, 50).Value = hh.HanhDong;
                cmd.Parameters.Add(Common.Constants.HangHoa.HangHoaID, SqlDbType.Int).Value = hh.HangHoaID;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaHangHoa, SqlDbType.VarChar, 50).Value = hh.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaNhomHang, SqlDbType.VarChar, 50).Value = hh.MaNhomHang;
                cmd.Parameters.Add(Common.Constants.HangHoa.TenNhomHang, SqlDbType.NVarChar, 200).Value = hh.TenNhomHang;
                cmd.Parameters.Add(Common.Constants.HangHoa.TenDonViTinh, SqlDbType.NVarChar, 200).Value = hh.TenDonViTinh;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaDonViTinh, SqlDbType.VarChar, 50).Value = hh.MaDonViTinh;
                cmd.Parameters.Add(Common.Constants.HangHoa.TenHangHoa, SqlDbType.NVarChar, 200).Value = hh.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaNhap, SqlDbType.Float).Value = hh.GiaNhap;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaBanBuon, SqlDbType.Float).Value = hh.GiaBanBuon;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaBanLe, SqlDbType.Float).Value = hh.GiaBanLe;
                cmd.Parameters.Add(Common.Constants.HangHoa.MaThue, SqlDbType.VarChar, 50).Value = hh.MaThue;
                cmd.Parameters.Add(Common.Constants.HangHoa.GiaTriThue, SqlDbType.Float).Value = hh.GiaTriThue;
                cmd.Parameters.Add(Common.Constants.HangHoa.MucDatHang, SqlDbType.Float).Value = hh.MucDatHang;
                cmd.Parameters.Add(Common.Constants.HangHoa.MucTonToiThieu, SqlDbType.Float).Value = hh.MucTonToiThieu;
                string linked = string.Empty;
                if (hh.LinkImage != string.Empty)
                {
                    linked = hh.MaHangHoa + ".jpeg";
                }
                else
                {
                    linked = string.Empty;
                }
                cmd.Parameters.Add(Common.Constants.HangHoa.LinkImage, SqlDbType.NVarChar, 200).Value = linked;
                cmd.Parameters.Add(Common.Constants.HangHoa.GhiChu, SqlDbType.NVarChar, 200).Value = hh.GhiChu;
                cmd.Parameters.Add(Common.Constants.HangHoa.Deleted, SqlDbType.Bit).Value = hh.Deleted;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    if (linked != string.Empty)
                    {
                        string links = @"\HangHoaImage\" + linked;
                        string linksRoot = System.Windows.Forms.Application.StartupPath.ToString() + links;
                        try { Common.Utilities.DeleteFile(links); }
                        catch { }
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(hh.Images);
                        Bitmap bmp = new Bitmap(ms);
                        try
                        {
                            bmp.Save(linksRoot, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        catch { }
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
        public static int Delete(Entities.HangHoa hh)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.deleteHangHoa, cn);
                cmd.Parameters.Add(Common.Constants.HangHoa.MaHangHoa, SqlDbType.VarChar).Value = hh.MaHangHoa;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    //truyen vao ten file anh
                    Common.Utilities.DeleteFile(hh.LinkImage);
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
    }
}
